

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>openchem.utils.utils &mdash; OpenChem 0.1 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation_instructions.html">Installation instructions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../installation_instructions.html#general-installation">General installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../installation_instructions.html#installation-with-docker">Installation with Docker</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../how_to_run_tutorial.html">How to define and train models</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#arguments-for-launch-py">Arguments for launch.py</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#arguments-for-run-py">Arguments for run.py</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#configuration-file">Configuration file</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#launching-jobs">Launching jobs</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/blocks.html">Tutorials and Recipes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/getting_started.html">Getting started with building models in OpenChem</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#loading-data">Loading data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#creating-pytorch-dataset">Creating PyTorch dataset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#creating-openchem-model-and-specifying-parameters">Creating OpenChem model and specifying parameters</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#training-the-model">Training the model</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html">GraphCNN for predicting logP</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#defining-node-attributes">Defining node attributes</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#loading-data">Loading data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#defining-model-architechture">Defining model architechture</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#training-and-evaluating-the-model">Training and evaluating the model</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html">Tox21 Challenge</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#loading-data">Loading data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#defining-evaluation-function">Defining evaluation function</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#defining-model-architechture">Defining model architechture</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#training-and-evaluating-the-model">Training and evaluating the model</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../api-docs/blocks.html">API documentation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/models.html">models</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.openchem_model">openchem_model</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.Smiles2Label">Smiles2Label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.Graph2Label">Graph2Label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.MoleculeProtein2Label">MoleculeProtein2Label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#vanilla-model">vanilla_model</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/modules.html">modules</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/modules.encoders.html">encoders</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#module-modules.encoders.openchem_encoder">openchem_encoder</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#module-modules.encoders.rnn_encoder">rnn_encoder</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#cnn-encoder">cnn_encoder</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#gcnn-encoder">gcnn_encoder</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/modules.embeddings.html">embeddings</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.embeddings.html#module-modules.embeddings.openchem_embedding">openchem_embedding</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.embeddings.html#module-modules.embeddings.basic_embedding">basic_embedding</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.embeddings.html#positional-embedding">positional_embedding</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/modules.mlp.html">mlp</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/layers.html">layers</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/layers.html#module-layers.conv_bn_relu">conv_bn_relu</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/layers.html#module-layers.gcn">gcn</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/data.html">data</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.smiles_data_layer">smiles_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#graph-data-layer">graph_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.smiles_protein_data_layer">smiles_protein_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.vanilla_data_layer">vanilla_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.smiles_enumerator">smiles_enumerator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.utils">utils</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/criterion.html">criterion</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/criterion.html#module-openchem.criterion.multitask_loss">multitask_loss</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/optimizer.html">optimizer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/optimizer.html#module-optimizer.openchem_optimizer">openchem_optimizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/optimizer.html#module-optimizer.openchem_lr_scheduler">openchem_lr_scheduler</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/utils.html">utils</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/utils.html#module-openchem.utils.graph">graph</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/utils.html#id1">utils</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/utils.html#logger">logger</a></li>
</ul>
</li>
</ul>
</li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">OpenChem</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>openchem.utils.utils</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for openchem.utils.utils</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">six</span> <span class="kn">import</span> <span class="n">string_types</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>

<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">glob</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">six</span>


<div class="viewcode-block" id="move_to_cuda"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.move_to_cuda">[docs]</a><span class="k">def</span> <span class="nf">move_to_cuda</span><span class="p">(</span><span class="n">sample</span><span class="p">):</span>
    <span class="c1"># copy-pasted from</span>
    <span class="c1"># https://github.com/pytorch/fairseq/blob/master/fairseq/utils.py</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">_move_to_cuda</span><span class="p">(</span><span class="n">maybe_tensor</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">torch</span><span class="o">.</span><span class="n">is_tensor</span><span class="p">(</span><span class="n">maybe_tensor</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">maybe_tensor</span><span class="o">.</span><span class="n">cuda</span><span class="p">()</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">maybe_tensor</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">_move_to_cuda</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">maybe_tensor</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">maybe_tensor</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">_move_to_cuda</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">maybe_tensor</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">maybe_tensor</span>

    <span class="k">return</span> <span class="n">_move_to_cuda</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_latest_checkpoint"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.get_latest_checkpoint">[docs]</a><span class="k">def</span> <span class="nf">get_latest_checkpoint</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="o">!=</span> <span class="p">[]:</span>
        <span class="n">list_of_files</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">&#39;*&#39;</span><span class="p">))</span>
        <span class="n">latest_file</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">list_of_files</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getctime</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">latest_file</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span></div>


<div class="viewcode-block" id="deco_print"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.deco_print">[docs]</a><span class="k">def</span> <span class="nf">deco_print</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="s2">&quot;*** &quot;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">):</span>
    <span class="c1"># copy-pasted from</span>
    <span class="c1"># github.com/NVIDIA/OpenSeq2Seq/blob/master/open_seq2seq/utils/utils.py</span>
    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">((</span><span class="n">start</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="n">offset</span> <span class="o">+</span> <span class="n">line</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="n">end</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">start</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="n">offset</span> <span class="o">+</span> <span class="n">line</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="n">end</span><span class="p">)</span></div>


<div class="viewcode-block" id="flatten_dict"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.flatten_dict">[docs]</a><span class="k">def</span> <span class="nf">flatten_dict</span><span class="p">(</span><span class="n">dct</span><span class="p">):</span>
    <span class="c1"># copy-pasted from</span>
    <span class="c1"># github.com/NVIDIA/OpenSeq2Seq/blob/master/open_seq2seq/utils/utils.py</span>
    <span class="n">flat_dict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">dct</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span> <span class="ow">or</span> \
           <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">string_types</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">bool</span><span class="p">):</span>
            <span class="n">flat_dict</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">key</span><span class="p">:</span> <span class="n">value</span><span class="p">})</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="n">flat_dict</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">key</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">k</span><span class="p">:</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">flatten_dict</span><span class="p">(</span><span class="n">dct</span><span class="p">[</span><span class="n">key</span><span class="p">])</span><span class="o">.</span><span class="n">items</span><span class="p">()})</span>
    <span class="k">return</span> <span class="n">flat_dict</span></div>


<div class="viewcode-block" id="nest_dict"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.nest_dict">[docs]</a><span class="k">def</span> <span class="nf">nest_dict</span><span class="p">(</span><span class="n">flat_dict</span><span class="p">):</span>
    <span class="c1"># copy-pasted from</span>
    <span class="c1"># github.com/NVIDIA/OpenSeq2Seq/blob/master/open_seq2seq/utils/utils.py</span>
    <span class="n">nst_dict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">flat_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="n">nest_keys</span> <span class="o">=</span> <span class="n">key</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
        <span class="n">cur_dict</span> <span class="o">=</span> <span class="n">nst_dict</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">nest_keys</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">nest_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">cur_dict</span><span class="p">:</span>
                <span class="n">cur_dict</span><span class="p">[</span><span class="n">nest_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">cur_dict</span> <span class="o">=</span> <span class="n">cur_dict</span><span class="p">[</span><span class="n">nest_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span>
        <span class="n">cur_dict</span><span class="p">[</span><span class="n">nest_keys</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">value</span>
    <span class="k">return</span> <span class="n">nst_dict</span></div>


<div class="viewcode-block" id="nested_update"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.nested_update">[docs]</a><span class="k">def</span> <span class="nf">nested_update</span><span class="p">(</span><span class="n">org_dict</span><span class="p">,</span> <span class="n">upd_dict</span><span class="p">):</span>
    <span class="c1"># copy-pasted from</span>
    <span class="c1"># github.com/NVIDIA/OpenSeq2Seq/blob/master/open_seq2seq/utils/utils.py</span>
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">upd_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">org_dict</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">org_dict</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="nb">dict</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Mismatch between org_dict and upd_dict &quot;</span> <span class="s2">&quot;at node </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">key</span><span class="p">))</span>
                <span class="n">nested_update</span><span class="p">(</span><span class="n">org_dict</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">value</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">org_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">org_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span></div>


<div class="viewcode-block" id="time_since"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.time_since">[docs]</a><span class="k">def</span> <span class="nf">time_since</span><span class="p">(</span><span class="n">since</span><span class="p">):</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">since</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">s</span> <span class="o">/</span> <span class="mi">60</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">-=</span> <span class="n">m</span> <span class="o">*</span> <span class="mi">60</span>

    <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%d</span><span class="s1">m </span><span class="si">%d</span><span class="s1">s&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span></div>


<div class="viewcode-block" id="identity"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.identity">[docs]</a><span class="k">def</span> <span class="nf">identity</span><span class="p">(</span><span class="nb">input</span><span class="p">):</span>
    <span class="k">return</span> <span class="nb">input</span></div>


<div class="viewcode-block" id="calculate_metrics"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.calculate_metrics">[docs]</a><span class="k">def</span> <span class="nf">calculate_metrics</span><span class="p">(</span><span class="n">predicted</span><span class="p">,</span> <span class="n">ground_truth</span><span class="p">,</span> <span class="n">metrics</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">metrics</span><span class="p">(</span><span class="n">ground_truth</span><span class="p">,</span> <span class="n">predicted</span><span class="p">)</span></div>


<div class="viewcode-block" id="check_params"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.check_params">[docs]</a><span class="k">def</span> <span class="nf">check_params</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">required_dict</span><span class="p">,</span> <span class="n">optional_dict</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">required_dict</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">optional_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span>

    <span class="k">for</span> <span class="n">pm</span><span class="p">,</span> <span class="n">vals</span> <span class="ow">in</span> <span class="n">required_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">pm</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">config</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> parameter has to be specified&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">pm</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">vals</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
                <span class="n">vals</span> <span class="o">=</span> <span class="n">string_types</span>
            <span class="k">if</span> <span class="n">vals</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">vals</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> <span class="n">config</span><span class="p">[</span><span class="n">pm</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">vals</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> has to be one of </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">pm</span><span class="p">,</span> <span class="n">vals</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">vals</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">vals</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> \
                    <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">config</span><span class="p">[</span><span class="n">pm</span><span class="p">],</span> <span class="n">vals</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> has to be of type </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">pm</span><span class="p">,</span> <span class="n">vals</span><span class="p">))</span>

    <span class="k">for</span> <span class="n">pm</span><span class="p">,</span> <span class="n">vals</span> <span class="ow">in</span> <span class="n">optional_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">vals</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
            <span class="n">vals</span> <span class="o">=</span> <span class="n">string_types</span>
        <span class="k">if</span> <span class="n">pm</span> <span class="ow">in</span> <span class="n">config</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">vals</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">vals</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> <span class="n">config</span><span class="p">[</span><span class="n">pm</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">vals</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> has to be one of </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">pm</span><span class="p">,</span> <span class="n">vals</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">vals</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">vals</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> \
                    <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">config</span><span class="p">[</span><span class="n">pm</span><span class="p">],</span> <span class="n">vals</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> has to be of type </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">pm</span><span class="p">,</span> <span class="n">vals</span><span class="p">))</span></div>

    <span class="c1"># for pm in config:</span>
    <span class="c1">#     if pm not in required_dict and pm not in optional_dict:</span>
    <span class="c1">#         raise ValueError(&quot;Unknown parameter: {}&quot;.format(pm))</span>


<div class="viewcode-block" id="cross_validation_split"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.cross_validation_split">[docs]</a><span class="k">def</span> <span class="nf">cross_validation_split</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">n_folds</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">split</span><span class="o">=</span><span class="s1">&#39;random&#39;</span><span class="p">,</span> <span class="n">stratified</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">folds</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">split</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;random&#39;</span><span class="p">,</span> <span class="s1">&#39;fixed&#39;</span><span class="p">]:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Invalid value for argument </span><span class="se">\&#39;</span><span class="s1">split</span><span class="se">\&#39;</span><span class="s1">: &#39;</span> <span class="s1">&#39;must be either </span><span class="se">\&#39;</span><span class="s1">random</span><span class="se">\&#39;</span><span class="s1"> or </span><span class="se">\&#39;</span><span class="s1">fixed</span><span class="se">\&#39;</span><span class="s1">.&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">split</span> <span class="o">==</span> <span class="s1">&#39;fixed&#39;</span> <span class="ow">and</span> <span class="n">folds</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;When </span><span class="se">\&#39;</span><span class="s1">split</span><span class="se">\&#39;</span><span class="s1"> is </span><span class="se">\&#39;</span><span class="s1">fixed</span><span class="se">\&#39;</span><span class="s1"> &#39;</span> <span class="s1">&#39;argument </span><span class="se">\&#39;</span><span class="s1">folds</span><span class="se">\&#39;</span><span class="s1"> must be provided.&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">split</span> <span class="o">==</span> <span class="s1">&#39;fixed&#39;</span><span class="p">:</span>
        <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">targets</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">folds</span><span class="p">)</span>
    <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>


<div class="viewcode-block" id="make_positions"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.utils.make_positions">[docs]</a><span class="k">def</span> <span class="nf">make_positions</span><span class="p">(</span><span class="n">tensor</span><span class="p">,</span> <span class="n">padding_idx</span><span class="p">,</span> <span class="n">left_pad</span><span class="p">):</span>
    <span class="c1"># Copy-pasted from</span>
    <span class="c1"># https://github.com/pytorch/fairseq/blob/master/fairseq/utils.py</span>
    <span class="sd">&quot;&quot;&quot;Replace non-padding symbols with their position numbers.</span>
<span class="sd">    Position numbers begin at padding_idx+1.</span>
<span class="sd">    Padding symbols are ignored, but it is necessary to specify whether padding</span>
<span class="sd">    is added on the left side (left_pad=True) or right side (left_pad=False).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">max_pos</span> <span class="o">=</span> <span class="n">padding_idx</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">tensor</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">make_positions</span><span class="p">,</span> <span class="s1">&#39;range_buf&#39;</span><span class="p">):</span>
        <span class="n">make_positions</span><span class="o">.</span><span class="n">range_buf</span> <span class="o">=</span> <span class="n">tensor</span><span class="o">.</span><span class="n">new</span><span class="p">()</span>
    <span class="n">make_positions</span><span class="o">.</span><span class="n">range_buf</span> <span class="o">=</span> <span class="n">make_positions</span><span class="o">.</span><span class="n">range_buf</span><span class="o">.</span><span class="n">type_as</span><span class="p">(</span><span class="n">tensor</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">make_positions</span><span class="o">.</span><span class="n">range_buf</span><span class="o">.</span><span class="n">numel</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">max_pos</span><span class="p">:</span>
        <span class="n">torch</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">padding_idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">max_pos</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="n">make_positions</span><span class="o">.</span><span class="n">range_buf</span><span class="p">)</span>
    <span class="n">mask</span> <span class="o">=</span> <span class="n">tensor</span><span class="o">.</span><span class="n">ne</span><span class="p">(</span><span class="n">padding_idx</span><span class="p">)</span>
    <span class="n">positions</span> <span class="o">=</span> <span class="n">make_positions</span><span class="o">.</span><span class="n">range_buf</span><span class="p">[:</span><span class="n">tensor</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">1</span><span class="p">)]</span><span class="o">.</span><span class="n">expand_as</span><span class="p">(</span><span class="n">tensor</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">left_pad</span><span class="p">:</span>
        <span class="n">positions</span> <span class="o">=</span> <span class="n">positions</span> <span class="o">-</span> <span class="n">mask</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> \
                    <span class="n">mask</span><span class="o">.</span><span class="n">long</span><span class="p">()</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">tensor</span><span class="o">.</span><span class="n">clone</span><span class="p">()</span><span class="o">.</span><span class="n">masked_scatter_</span><span class="p">(</span><span class="n">mask</span><span class="p">,</span> <span class="n">positions</span><span class="p">[</span><span class="n">mask</span><span class="p">])</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Mariya Popova

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    

  <style>
    /* Sidebar header (and topbar for mobile) */
    .wy-side-nav-search, .wy-nav-top {
      background: #99badd;
    }
  </style>


</body>
</html>